<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right" style="font-size: 16px;">
      <el-breadcrumb-item :to="{ path: '/' }">
        <i class="el-icon-s-promotion"></i> 后台管理
      </el-breadcrumb-item>
      <el-breadcrumb-item>服务种类列表</el-breadcrumb-item>
      <el-breadcrumb-item v-for="item in history" :key="item.id"><span v-text="item.name"></span></el-breadcrumb-item>
    </el-breadcrumb>

    <el-divider></el-divider>
    <template>
      <el-card>
        <el-form :inline="true" :model="formInline" class="demo-form-inline">
          <el-form-item label="服务种类名称:">
            <el-input v-model="formInline.name" placeholder="名称"></el-input>
          </el-form-item>
          <el-form-item label="服务种类简介:">
            <el-input v-model="formInline.description" placeholder="简介" style="width: 460px"></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="onSubmit">新增</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </template>
    <el-card style="margin-top: 20px">
      <el-table :data="tableData" border style="width: 100%;">
        <el-table-column prop="id" label="ID" width="80" align="center"></el-table-column>
        <el-table-column prop="name" label="服务种类名称" width="180" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column prop="description" label="服务种类简介" header-align="center"
                         :show-overflow-tooltip="true"></el-table-column>
        <el-table-column label="操作" width="100" align="center">
          <template slot-scope="scope">
            <el-button type="primary" icon="el-icon-edit" circle size="mini"
                       @click="openEditDialog(scope.row),dialogFormVisible2=true"></el-button>
            <el-button type="danger" icon="el-icon-delete" circle size="mini"
                       :disabled="scope.row.isParent == 1"
                       @click="openDeleteConfirm(scope.row)"></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
    <div v-if="currentParentId != 0" style="text-align: right; margin: 10px 0;">
      <el-button @click="goBack()">返回</el-button>
    </div>

    <!--   编辑修改-->
    <el-dialog title="修改信息" :visible.sync="dialogFormVisible2">
      <el-form :model="editForm">
        <el-form-item label="种类名称" :label-width="formLabelWidth">
          <el-input v-model="editForm.name" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="种类简介" :label-width="formLabelWidth">
          <el-input v-model="editForm.description" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible2 = false">取 消</el-button>
        <el-button type="primary" @click="updated() ">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      formInline: {
        name: '',
        description: ''
      },
      tableData: [],
      currentParentId: 0,
      history: [],
      dialogFormVisible2: false,
      form: [],
      editForm: {
        id: '',
        name: '',
        description: ''
      },
      formLabelWidth: '120px'
    }
  },
  methods: {
    onSubmit() {
      let url = 'http://localhost:9081/serveKinds/add-new';
      let serveKinds = this.formInline;
      console.log(serveKinds)
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, serveKinds).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '新增类别成功！',
            type: 'success'
          });
          this.loadCategoryList();
          this.formInline.name = '';
          this.formInline.description = '';
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    updated() {
      let url = 'http://localhost:9081/serveKinds/' + this.editForm.id + '/update';
      console.log('url = ' + url);
      let formData = this.qs.stringify(this.editForm);
      console.log('formData = ' + formData);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url, formData).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '修改类别成功！',
            type: 'success'
          });
          this.loadCategoryList();
          this.dialogFormVisible2 = false;
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    goBack() {
      let parentCategory = this.history[this.history.length - 1];
      this.history.pop();
      this.currentParentId = parentCategory.parentId;
      this.loadCategoryList();

      for (let i = 0; i < this.history.length; i++) {
        console.log(this.history[i].name);
      }
      console.log('-----------');
    },
    showSubCategoryList(category) {
      this.history[category.depth - 1] = category;
      this.currentParentId = category.id;
      this.loadCategoryList();

      for (let i = 0; i < this.history.length; i++) {
        console.log(this.history[i].name);
      }
      console.log('-----------');
    },
    updateEnable(category) {
      let enableStatus = [
        {'url': '/disable', text: '禁用'},
        {'url': '/enable', text: '启用'}
      ];
      let url = 'http://localhost:9081/categories/'
          + category.id + enableStatus[category.enable].url;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: enableStatus[category.enable].text + '类别成功！',
            type: 'success'
          });
          this.loadCategoryList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    openEditDialog(category) {
      let url = 'http://localhost:9081/serveKinds/' + category.id;
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.dialogFormVisible2 = true;
          this.editForm = responseBody.data;
        } else {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        }
      });
    },
    openDeleteConfirm(category) {
      let message = '此操作将永久删除【' + category.name + '】类别，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(category);
      }).catch(() => {
      });
    },
    handleDelete(category) {
      let url = 'http://localhost:9081/serveKinds/' + category.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let responseBody = response.data;
        if (responseBody.state == 20000) {
          this.$message({
            message: '删除类别成功！',
            type: 'success'
          });
          this.loadCategoryList();
        } else if (responseBody.state == 40400) {
          this.$alert(responseBody.message, '警告', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadCategoryList();
            }
          });
        } else {
          this.$message.error(responseBody.message);
        }
      });
    },
    loadCategoryList() {
      let url = 'http://localhost:9081/serveKinds/list-by-parent';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let responseBody = response.data;
        this.tableData = responseBody.data;
      });
    }
  },
  mounted() {
    this.loadCategoryList();
  }
}
</script>

<style scoped>

</style>